﻿using System;
using System.Web.UI.WebControls;
using Core.Library;
using DataObject;
using DataObject.Article;
using DataObject.Category;

namespace KMT.Controls.Article
{
    public partial class Listing : System.Web.UI.UserControl
    {
        protected int CategoryId;
        protected int PageIndex;
        protected int PageSize;

        protected void Page_Load(object sender, EventArgs e)
        {
            CategoryId = Utils.GetIdFromRequestString(Utils.CategoryId, 0);
            PageIndex = Utils.GetIdFromRequestString(Utils.PageIndex, SettingValues.PageIndex);
            PageSize = Utils.GetIdFromRequestString(Utils.PageSize, SettingValues.PageSize_5);

            if (!IsPostBack)
            {
                ListPagingArticle();
            }
        }

        public void ListPagingArticle()
        {
            var totalRecords = 0;
            var lstArticle = ArticleController.Article_GetListOrderByPublishOn(CategoryId, PageIndex, PageSize, ref totalRecords);

            var categoryName = CategoryController.GetCategoryName(CategoryId);

            if (lstArticle.Count <= 0) return;
            rptNews.DataSource = lstArticle;
            rptNews.DataBind();

            ltPager.Text = PagerUtility.PageLinks(PageIndex, PageSize, totalRecords, SettingValues.PageStep,
                                                  UrlHelpers.ListArticleByCategoryPagination(CategoryId, UnicodeUtility.UnicodeToKoDauAndGach(categoryName).ToLower(), "{0}"));
            ltCategoryName.Text = categoryName;

            Page.Title = string.Format(".: {1} {0} :.", Constants.SITE_TITLE, categoryName);
        }

        protected void rptNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.AlternatingItem && e.Item.ItemType != ListItemType.Item) return;

            var categoryId = ConvertUtility.ToInt32(((HiddenField)e.Item.FindControl("hfCategoryId")).Value);
            var articleId = ConvertUtility.ToInt32(((HiddenField)e.Item.FindControl("hfArticleId")).Value);
            var title = ((HiddenField)e.Item.FindControl("hfTitle")).Value;

            var ltCategory = (Literal)e.Item.FindControl("ltCategory");
            var ltTitle = (Literal)e.Item.FindControl("ltTitle");
            var ltImage = (Literal)e.Item.FindControl("ltImage");
            var ltView = (Literal)e.Item.FindControl("ltView");

            var categoryName = CategoryController.GetCategoryName(categoryId);

            var cHref = UrlHelpers.ListArticleByCategory(categoryId, categoryName);
            var aHref = UrlHelpers.ArticleDetail(categoryName, ConvertUtility.ToInt32(articleId), title);
                
            var imgHref = string.Format("{0}/Thumb.ashx?Id={1}&tId={2}&Size=765", Constants.ThumbImage, articleId, (int)CategoryType.Article);

            ltCategory.Text = string.Format("<a href=\"{0}\" class=\"black under\">{1}</a>", cHref, categoryName);
            ltTitle.Text = string.Format("<a href=\"{0}\">{1}</a>", aHref, title);
            ltImage.Text = string.Format("<a href=\"{0}\" class=\"article-image\" style=\"background-image: url('{1}')\"></a>", aHref, imgHref);
            ltView.Text = string.Format("<a href=\"{0}\" class=\"buttonone right\" style=\"margin-top: 4px\">Xem chi tiết</a>", aHref);
        }
    }
}